package com.tledu.carUse.service.impl;

import com.tledu.car.mapper.CarListMapper;
import com.tledu.carLog.domain.CarLog;
import com.tledu.carLog.mapper.CarLogMapper;
import com.tledu.carUse.domain.CarUse;
import com.tledu.carUse.domain.dto.CarUseDto;
import com.tledu.carUse.domain.vo.CarUseVo;
import com.tledu.carUse.mapper.CarUseMapper;
import com.tledu.carUse.service.ICarUseService;
import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 我的用车Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class CarUseServiceImpl implements ICarUseService {
    @Autowired
    private CarUseMapper carUseMapper;
    @Resource
    private CarLogMapper carLogMapper;
    @Resource
    private CarListMapper carListMapper;

    /**
     * 查询我的用车
     *
     * @param id 我的用车主键
     * @return 我的用车
     */
    @Override
    public CarUse selectCarUseById(Long id) {
        return carUseMapper.selectCarUseById(id);
    }

    @Override
    public CarUse selectCarUseBycarId(CarUseVo carUseVo) {
        return carUseMapper.selectCarUseBycarId(carUseVo);
    }


    /**
     * 查询我的用车列表
     *
     * @param carUse 我的用车
     * @return 我的用车
     */
    @Override
    public List<CarUse> selectCarUseList(CarUseDto carUseDto) {
        return carUseMapper.selectCarUseList(carUseDto);
    }

    /**
     * 新增我的用车
     *
     * @param carUse 我的用车
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCarUse(CarUse carUse) {
        carUse.setCreateTime(DateUtils.getNowDate());
        carUse.setCreateUser(SecurityUtils.getUserId());
        CarLog carLog = new CarLog();
        carLog.setOperateType(3L);
        carLog.setCreateTime(DateUtils.getNowDate());
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setCarId(carUse.getCarId());
        carLogMapper.insertCarLog(carLog);
        return carUseMapper.insertCarUse(carUse);
    }

    /**
     * 修改我的用车
     *
     * @param carUse 我的用车
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCarUse(CarUse carUse) {
        carUse.setReturnTime(DateUtils.getNowDate());
        CarLog carLog = new CarLog();
        carLog.setOperateType(4L);
        carLog.setCreateUser(SecurityUtils.getUserId());
        carLog.setCarId(carUse.getCarId());
        carLog.setReturnTime(carUse.getReturnTime());
        carLogMapper.insertCarLog(carLog);
        return carUseMapper.updateCarUse(carUse);
    }

    /**
     * 移除我的用车
     *
     * @param id 我的用车
     * @return 结果
     */
    @Override
    public int removeCarUse(long id) {
        return carUseMapper.removeCarUse(id);
    }

    /**
     * 批量删除我的用车
     *
     * @param ids 需要删除的我的用车主键
     * @return 结果
     */
    @Override
    public int deleteCarUseByIds(Long[] ids) {
        return carUseMapper.deleteCarUseByIds(ids);
    }

    /**
     * 删除我的用车信息
     *
     * @param id 我的用车主键
     * @return 结果
     */
    @Override
    public int deleteCarUseById(Long id) {
        return carUseMapper.deleteCarUseById(id);
    }
}
